<template>
  <div>
    <PageTools>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="型号搜索">
          <el-input v-model="formInline.name" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="Search"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </PageTools>
    <el-card class="main">
      <el-button
        style="margin-bottom: 10px"
        type="primary"
        size="small"
        icon="el-icon-circle-plus-outline"
        @click="addForm"
      >新建</el-button>
      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%"
        :header-cell-style="{
          fontWeight: 'normal',
          background: '#f3f6fb',
        }"
      >
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column label="型号名称" prop="name" />
        <el-table-column label="型号编号" prop="model" />
        <el-table-column label="设备图片" prop="image">
          <template #default="{ row }">
            <div class="imgRow">
              <img :src="row.image" class="vm-type-image">
            </div>
          </template>
        </el-table-column>
        <el-table-column label="货道行" prop="vmRow" />
        <el-table-column label="货道列" prop="vmCol" />
        <el-table-column label="设备容量" prop="channelMaxCapacity" />
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="small"
              @click="detailsFn(row)"
            >修改</el-button>
            <el-button type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row
        v-if="totalPage !== 1"
        type="flex"
        justify="space-between"
        align="middle"
      >
        <el-col :span="19">
          <p class="paginationHeader">
            {{ `共${totalCount}条记录 第${page.pageIndex}/${totalPage}页` }}
          </p>
        </el-col>
        <el-col :span="5">
          <el-pagination
            background
            :current-page="page.pageIndex"
            :page-size="page.pageSize"
            :total="totalCount"
            layout="prev, next"
            prev-text="上一页"
            next-text="下一页"
            @current-change="changePage"
          />
        </el-col>
      </el-row>
    </el-card>
    <!--新增设备类型-->
    <el-dialog
      title="新增设备类型"
      :visible="dialogType"
      width="45%"
      @open="opneDialog"
      @close="closeDialog"
    >
      <el-form
        ref="TypeRef"
        label-width="120px"
        :model="TypeForm"
        :rules="TypeFormRules"
      >
        <el-form-item label="型号名称" prop="name">
          <el-input
            v-model="TypeForm.name"
            type="text"
            placeholder="请输入内容"
            maxlength="10"
            show-word-limit
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="型号编码" prop="model">
          <el-input
            v-model="TypeForm.model"
            type="text"
            placeholder="请输入(限制数字、字母、中划线、下划线)"
            maxlength="15"
            show-word-limit
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="货道行数" prop="vmRow">
          <el-input-number
            v-model="TypeForm.vmRow"
            style="width: 80%"
            controls-position="right"
            :min="0"
            :max="100"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="货道列数" prop="vmCol">
          <el-input-number
            v-model="TypeForm.vmCol"
            style="width: 80%"
            controls-position="right"
            :min="0"
            :max="100"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="货道容量" prop="channelMaxCapacity">
          <el-input-number
            v-model="TypeForm.channelMaxCapacity"
            style="width: 80%"
            controls-position="right"
            :min="0"
            :max="100"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="设备图片" prop="image">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :http-request="upload"
            :before-upload="beforeAvatarUpload"
          >
            <!-- 上传后展示的图片 -->
            <img v-if="TypeForm.image" :src="TypeForm.image" class="avatar">
            <!-- 默认的加号图片 -->
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
          <span>  {{ `支持扩展名：jpg、png，文件不得大于100kb` }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          class="cancel"
          @click="closeDialog"
        >取 消</el-button>
        <el-button
          type="primary"
          class="sure"
          @click="TypeConfig"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { vmTypeList, addvmType, updatavmType } from '@/api/vm'
export default {
  data() {
    return {
      formInline: { name: '' },
      loading: false,
      list: [],
      page: {
        pageIndex: 1,
        pageSize: 10
      },
      totalCount: 0,
      totalPage: 0,
      // 新增设备类型
      dialogType: false,
      TypeForm: {
        name: '',
        model: '',
        vmRow: '', // 行数
        vmCol: '', // 列数
        channelMaxCapacity: '',
        image: ''
      },
      TypeFormRules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        model: [{ required: true, message: '请输入', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_-]*$/, message: '请输入正确内容', trigger: 'blur' }],
        vmRow: [{ required: true, message: '请输入', trigger: 'change' }], // 行数
        vmCol: [{ required: true, message: '请输入', trigger: 'change' }], // 列数
        channelMaxCapacity: [{ required: true, message: '请输入', trigger: 'change' }],
        image: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.vmTypeList()
  },
  methods: {
    async vmTypeList() {
      this.loading = true
      const { data } = await vmTypeList(this.page)
      this.list = data.currentPageRecords
      this.loading = false
      this.totalCount = Number(data.totalCount)
      this.totalPage = Number(data.totalPage)
    },
    // 1.查询
    Search() {},
    // 2.新增
    addForm() {
      this.dialogType = true
    },
    // 3.分页
    changePage(val) {
      this.page.pageIndex = val
      this.vmTypeList()
    },
    // 4.查看详情
    detailsFn(row) {
      this.dialogType = true
      this.TypeForm = row
    },
    // 设备类型弹出层
    opneDialog() {},
    closeDialog() {
      this.$refs.TypeRef.resetFields()
      this.TypeForm = {
        name: '',
        model: '',
        vmRow: '', // 行数
        vmCol: '', // 列数
        channelMaxCapacity: '',
        image: ''
      }
      this.dialogType = false
    },
    // 弹出层确定
    TypeConfig() {
      this.$refs.TypeRef.validate(async(isOk) => {
        if (!isOk) return
        if (this.TypeForm.typeId) {
          await updatavmType(this.TypeForm)
        } else {
          await addvmType(this.TypeForm)
        }
        this.dialogType = false
        this.vmTypeList()
      })
    },
    // 图片校验
    beforeAvatarUpload(file) {
      const fileList = [
        'image/jpeg',
        'image/png'
      ]
      const isType = fileList.includes(file.type)
      const isLt100kb = file.size / 1024 < 100
      if (!isType) {
        this.$message.error('上传头像图片只能是 PNG 格式!')
      }
      if (!isLt100kb) {
        this.$message.error('上传头像图片大小不能超过 100kB!')
      }
      return isType && isLt100kb
    },
    upload(file) {
      console.log(file)
      this.TypeForm.image = `http://likede2-java.itheima.net/image/${file.file.name}`
    }
  }
}
</script>

<style lang="scss">
.imgRow {
  .vm-type-image {
    display: block;
    background-color: pink;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    object-fit: cover;
  }
}
.el-dialog .el-input .el-input__inner{
  height: 40px !important;
}
</style>
